package com.xinchuang.controller.image;

import com.xinchuang.service.image.MercatorWmtsService;
import com.xinchuang.utils.FileLoader;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "地图发布")
@RequestMapping("/mercator")
@RequiredArgsConstructor
public class MercatorWmtsController {

    private final MercatorWmtsService mercatorWmtsService;

    private final FileLoader loader;

    @GetMapping(value = "/wmts", produces = MediaType.IMAGE_PNG_VALUE)
    public ResponseEntity<?> getByMercator(
            @RequestParam(value = "x", required = false) Integer x,
            @RequestParam(value = "y", required = false) Integer y,
            @RequestParam(value = "z", required = false) Integer z,
            @RequestParam(value = "id", required = false) String id
    ) {
//        if (null == x || null == y || null == z || StringUtils.isNotBlank(id)) {
//            try {
//                return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(loader.readFileAsBytes("404.png"));
//            } catch (IOException e) {
//                e.printStackTrace();
//                return ResponseEntity.notFound().build();
//            }
//        }
        byte[] bytes = mercatorWmtsService.getTile(x, y, z, id);
        if (null == bytes) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(bytes);
    }
}
